<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  <style>
    #todo{
      width: 600px;
      padding: 15px;
      border: 1px solid #333;
      margin: 50px auto;
      border-radius: 8px;
    }
    .todo-content{
      margin-top: 30px;
    }
    .bg-done{
      background-color: rgb(173, 169, 169);
    }
    .bg-undone{
      background-color: rgb(158, 93, 93);
    }
    .ani1-enter-from{
      transform: translateX(-100%);
      opacity: 0;
    }
    .ani1-leave-to{
      transform: translateX(100%);
      opacity: 0;
    }
    .ani1-enter-active,.ani1-leave-active{
      transition: all 500ms;
    }
  </style>
</head>
<body>
  <div id="app">
    <div id="todo">
      <div class="todo-header">
        <div class="row">
          <div class="col-md-10">
            <input type="text" v-model.trim="inputTxt" class="form-control">
          </div>
          <div class="col-md-2">
            <button class="btn btn-primary" @click="addTodo">增加</button>
          </div>
        </div>
      </div>
      <div class="todo-content">
        <ul class="list-group">
          <li class="list-group-item text-center" v-if="todos.length===0">
            暂无待办事项
          </li>
          <transition-group name="ani1">
            <li 
              v-for="(todo, index) in todos"
              :key="index"
              :class="['list-group-item', {
                'bg-done': todo.done,
                'bg-undone': !todo.done
              }]">
              <span>
                {{todo.content}}
              </span>
              <div class="pull-right">
                <button class="btn btn-danger btn-xs" @click="delTodo(index)">删除</button>
                <button class="btn btn-xs btn-success" @click="setTodoDone(index)" :disabled="todo.done">
                  {{ todo.done?'已完成':'点击完成' }}
                </button>
              </div>
            </li>
          </transition-group>
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
      data(){
        return {
          // 绑定输入框value 新增待办事项
          inputTxt: '',
          todos: [
           
          ]
        }
      },
      methods: {
        // 新增待办事项
        addTodo(){
          // 输入框内容为空 不能新增
          if (this.inputTxt === '') {
            return false;
          }
          this.todos.push({
            content: this.inputTxt,
            done: false
          })
          // 清空输入框内容
          this.inputTxt = '';
        },
        delTodo(index){
          // 删除待办事项
          if(confirm('您确认删除吗?')) {
            this.todos.splice(index, 1);
          }
        },
        setTodoDone(index) {
          // 设置已完成
          this.todos[index].done = true
        }
      }
    })
    app.mount('#app')
  </script>
</body>
</html>